package com.sqz.service.impl;

import com.sqz.dao.RoleDao;
import com.sqz.pojo.RightsEntity;
import com.sqz.pojo.RolerightsEntity;
import com.sqz.pojo.RolesEntity;
import com.sqz.pojo.ext.RoleRightExt;
import com.sqz.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    public List<RoleRightExt> roleList() {

        List<RoleRightExt> roleRightExts = new ArrayList<RoleRightExt>();
        List<RolesEntity> rolses = roleDao.selectAllRole();

        for (RolesEntity role : rolses) {
            List<String> rights = roleDao.selectRightsByRoleId(role.getId());
            roleRightExts.add(new RoleRightExt(role.getId(), role.getName(), rights));
        }
        return roleRightExts;
    }

    public List<RightsEntity> allRights() {

        List<RightsEntity> allRoleName = roleDao.selectAllRights();

        return allRoleName;
    }

    @Override
    public String addRole(String roleName, String rights) {

        RolesEntity rolesEntity = new RolesEntity();
        rolesEntity.setName(roleName);
        roleDao.addRole(rolesEntity);
        String[] strings = rights.split(",");
        for (String rightId : strings) {
            roleDao.addRoleRight(new RolerightsEntity(rolesEntity.getId(), Integer.parseInt(rightId)));
        }


        return null;
    }

}
